package edu.bu.cs673.superkids123.children.DataObjectAccess;

import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Restrictions;

import edu.bu.cs673.superkids123.HibernateUtil;
import edu.bu.cs673.superkids123.children.model.CompletedGame;
import edu.bu.cs673.superkids123.children.model.CompletedLesson;
import edu.bu.cs673.superkids123.children.model.Lesson;
import edu.bu.cs673.superkids123.children.model.Level;
import edu.bu.cs673.superkids123.common.model.Achievement;
import edu.bu.cs673.superkids123.common.model.Child;

public class ReportManager {

	/**
	 * Add Completed Lesson
	 * 
	 * @author Adam Phung
	 * @author Abdelghani Bhalla
	 * @param CompletedLesson
	 * @return -1 for error
	 */
	public static int addCompletedLesson(CompletedLesson lesson)
	{
		int result = -1;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {
			tx = session.beginTransaction();
			result = (Integer) session.save(lesson);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
	
	/**
	 * Gets all lessons (completed or not) report for the given child
	 * @param child
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<CompletedLesson> getAllCompletedLessons(Child child) {
		List<CompletedLesson> completedLessons = null;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();
		int childId = child.getId();
		try {

			tx = session.beginTransaction();
			Query query = session.createQuery("From CompletedLesson where UserID=:userId");
			query.setParameter("userId", childId);
			completedLessons = (List<CompletedLesson>) query.list();

			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

		return completedLessons;
	}
	
	/**
	 * Get All Lessons that were completed | uncompleted By Child
	 * @param child
	 * @param isCompleted , pass true to return all completed lesson, or false to return all uncompleted lessons
	 */
	@SuppressWarnings("unchecked")
	public static List<CompletedLesson> getAllChildCompletedLessons(Child child, boolean isCompleted)
	{
		List<CompletedLesson> completedLessons = null;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();
		int childId = child.getId();
		try {

			tx = session.beginTransaction();
			Query query = session.createQuery("From CompletedLesson where IsCompleted=:isCompleted and UserID=:userId");
			query.setParameter("isCompleted", isCompleted);
			query.setParameter("userId", childId);
			completedLessons = (List<CompletedLesson>) query.list();

			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

		return completedLessons;
	}
	
	
	/**
	 * Gets all completed games (finished or not) for the given child
	 * 
	 * @param child
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<CompletedGame> getAllCompletedGames(Child child) {
		List<CompletedGame> completedGames = null;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();
		int childId = child.getId();
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("From CompletedGame where UserID=:userId");
			query.setParameter("userId", childId);
			completedGames = (List<CompletedGame>) query.list();

			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

		return completedGames;
	}
	
	/**
	 * Get All Games that were completed | uncompleted By Child
	 * @param child
	 * @param isCompleted , pass true to return all completed lesson, or false to return all uncompleted lessons
	 */
	@SuppressWarnings("unchecked")
	public static List<CompletedGame> getAllChildCompletedGames(Child child, boolean isCompleted)
	{
		List<CompletedGame> completedGames = null;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();
		int childId = child.getId();
		try {

			tx = session.beginTransaction();
			Query query = session.createQuery("From CompletedGame where IsCompleted=:isCompleted and UserID=:userId");
			query.setParameter("isCompleted", isCompleted);
			query.setParameter("userId", childId);
			completedGames = (List<CompletedGame>) query.list();

			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

		return completedGames;
	}
	/*
	 * Add Complete game
	 * Param: complete game
	 */
	public static int addCompletedGame(CompletedGame game)
	{
		int result = -1;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {
			tx = session.beginTransaction();
			result = (Integer) session.save(game);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}
}
